﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nature.Numerics
{
    public interface INxN
    {
        double this[int i, int j] { get; set; }

        int Nva { get; }
    }

    public static partial class Extensions
    {
        public static void Solve(this INxN matrix, double[] rhs)
        {
            matrix.Solve(new Vector(rhs));
        }

        public static void Assign<TMatrix>(this TMatrix lhs, Func<int, int, double> rhs)
            where TMatrix : INxN
        {
            for (int i = 0; i < lhs.Nva; ++i)
            {
                for (int j = 0; j < lhs.Nva; ++j)
                {
                    lhs[i, j] = rhs(i, j);
                }
            }
        }

        public static void Assign<TMatrix>(this TMatrix lhs, double[][] rhs)
            where TMatrix : INxN
        {
            MMAssign(lhs, new NxN(rhs));
        }


        public static void Assign<TMatrix1, TMatrix2>(this TMatrix1 lhs, TMatrix2 rhs)
            where TMatrix1 : INxN
            where TMatrix2 : INxN
        {
            MMAssign(lhs, rhs);
        }


        private static void MMAssign<TMatrix1, TMatrix2>(this TMatrix1 lhs, TMatrix2 rhs)
            where TMatrix1 : INxN
            where TMatrix2 : INxN
        {
            if (lhs == null || rhs == null)
            {
                throw new System.ArgumentNullException();
            }
            if (lhs.Nva != rhs.Nva)
            {
                throw new System.ArgumentOutOfRangeException();
            }
            for (int i = 0; i < lhs.Nva; ++i)
            {
                for (int j = 0; j < lhs.Nva; ++j)
                {
                    lhs[i, j] = rhs[i, j];
                }
            }
        }
    }
}
